package cn.luckdou.commons.utils;

import org.apdplat.word.WordSegmenter;
import org.apdplat.word.segmentation.SegmentationAlgorithm;
import org.apdplat.word.segmentation.Word;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 拆词器（将输入的单词 拆分成多个单词）
 * Created by 杨 on 2018/3/7.
 */
public class ParticipleUtils {
    /**
     * 对语句进行分词(Ik分词器）
     *
     * @param text 语句
     * @return 分词后的集合
     * @throws IOException
     */
    public static List<String> segment(String text){
        try {
            text.replaceAll(" ","");
            List<String> list = new ArrayList<String>();
            list.add(text);
            StringReader re = new StringReader(text);
            IKSegmenter ik = new IKSegmenter(re, true);
            Lexeme lex;
            while ((lex = ik.next()) != null) {
                list.add(lex.getLexemeText());
            }
            return list;
        }catch (IOException e){
            e.printStackTrace();
            return null;
        }
    }

    public static List<String> addEm(List<String> segments,List<String> texts){
        List<String> result=new ArrayList<>();

        String[] fs=new String[segments.size()];
        String[] ft=new String[texts.size()];
        for(int i=0;i<segments.size();i++) fs[i]=segments.get(i);
        for(int i=0;i<texts.size();i++) ft[i]=texts.get(i);

        for (int i=0;i<segments.size();i++)
            for(int j=0;j<texts.size();j++)
                ft[j]=ft[j].replaceAll(fs[i],"<em>"+fs[i]+"</em>");

        for(int i=0;i<texts.size();i++)
            ft[i]=ft[i].replaceAll("</em><em>","");

        String s;
        for (int i=0;i<texts.size()-1;i++)
            for(int j=i+1;j<texts.size();j++)
            if (EmNum(ft[i])<EmNum(ft[j])){
                s=ft[i];
                ft[i]=ft[j];
                ft[j]=s;
            }

        for(int i=0;i<texts.size();i++)  result.add(ft[i]);
        return result;
    }

    private static int EmNum(String s){
        int i=0;
        int j=0;
        int  ans=0;
        while (true) {
            i=s.indexOf("<em>");
            j=s.indexOf("</em>");
            if (i<0 || j<0) return ans;
            ans+=(j-i-4);
            if (ans>=4){
                System.out.println();
            }
            s=s.substring(j+5,s.length());
        }
    }

    public static void main(String[] args) {
        List<Word> words = WordSegmenter.seg("乍得2016CNPCIC公司Ximenia&Kedeni区块采集项目", SegmentationAlgorithm.BidirectionalMinimumMatching);
        System.out.println(words);
    }

//    public static List<String> Dsegment(String text) {
//        List<String> list = new ArrayList<String>();
//        text=text.trim();
//        if (text.length()<=0) return list;
////        String k="";
////        for (int i=0;i<text.length()-1;i++)
////            for(int j=i;j<text.length();j++){
////                k=text.substring(i,j);
////                if (k.trim().length()>0)
////                    list.add(k);
////            }
//
//
//        for (int i=0;i<text.length()-1;i++)
//            if (text.substring(i,i+1).trim().length()>0)
//                list.add(text.substring(i,i+1).trim());
////
////        List<Word> words = WordSegmenter.seg(text, SegmentationAlgorithm.BidirectionalMinimumMatching);
////        for (Word word : words) {
////            list.add(word.getText());
////        }
//
//        return list;
//    }


}
